package com.dzt.trainingmgt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin(origins = "*") // 允许所有来源的跨域请求
public class HelloController {

    // 修改映射路径为 /api/login，并指定接收 POST 请求
    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody LoginRequest loginRequest) {
        // 打印接收到的 id 和 password
        System.out.println("Received ID: " + loginRequest.getId());
        System.out.println("Received Password: " + loginRequest.getPassword());

        // 这里可以进行身份验证逻辑，现在我们只是返回一个固定的角色
        return "{\"role\":\"员工\"}";
    }

    // 添加一个内部类来表示登录请求的 JSON 结构
    public static class LoginRequest {
        private String id;
        private String password;

        // Getter 和 Setter 方法
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
